<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发票详情</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .invoice-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div,
        td[data-field="negativeImgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .invoice-container {
            background-color: #fff;
        }

        .bar {
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
        }

        .searchInput {
            display: flex;
            align-items: flex-end;
        }

        .searchInput label {
            font-size: 12px;
        }

        .searchInput input {
            width: 200px;
        }
    </style>
</head>

<body>
    <div class="invoice-container">
        <!-- 搜索 -->
        <div class="searchInput">
            <form class="layui-form" action="" lay-filter="resets">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 85px;">开票人姓名：</label>
                    <input class="layui-input" name="receiptUserName" placeholder="请输入开票人姓名" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 85px;">开票人手机号：</label>
                    <input class="layui-input" name="phone" placeholder="请输入开票人手机号" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">筛选日期：</label>
                    <input type="text" class="layui-input" id="test1" name="createTimeStart" placeholder="请输入查询日期"
                        autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
                <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit=""
                    lay-filter="reset">重置</button>
            </form>
        </div>

        <form class="layui-form">
            <!-- 筛选 -->
            <div class="layui-inline">
                <div class="layui-form-item" id="status" style="margin-bottom:0">
                    <label class="layui-form-label"
                        style="text-align:right; font-size:14px;padding: 9px 0; font-size:12px;">开票状态：</label>
                    <div class="layui-input-block" style="margin-left: 90px; margin-top: 16px;">
                        <select name="receiptStatus" lay-filter="select">
                            <option value="">全部</option>
                            <option value="1">已开发票</option>
                            <option value="0">提交发票申请</option>
                        </select>
                    </div>
                </div>
            </div>

        </form>

        <table lay-filter="invoiceTable" class="layui-hide" id="invoiceTable"></table>
    </div>

</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="lookDetails">编辑</a>
</script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery', "laydate"], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery;

        table.render({
            elem: '#invoiceTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/orderReceipt/getOrderReceiptList'
            , headers: {
                Authorization: localStorage.token
            }
            , method: 'post'
            , contentType: 'application/json'
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            , request: {
                pageName: 'start' //页码的参数名称，默认：page
                , limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , cols: [[
                { field: 'receiptCompany', title: '开票公司名称' }
                , { field: 'receiptPrice', title: '发票金额(元)' }
                , { field: 'receiptTaxpayerNum', title: '纳税人识别号' }
                , { field: 'receiptBrandName', title: '开户行名称' }
                , { field: 'receiptBrandNum', title: '银行账号' }
                , {
                    field: 'receiptType', title: '发票类型', templet: function (res) {
                        return res.receiptType == 1 ? "增值税专用发票" : res.receiptType == 2 ? "增值税普通发票" : "-"
                    }
                }
                , {
                    field: 'orderType', title: '商品类型', templet: function (res) {
                        return res.orderType == 0 ? "商品订单" : res.orderType == 1 ? "分销商订单" : "-"
                    }
                }
                , {
                    field: 'receiptStatus', title: '状态', templet: function (res) {
                        return res.receiptStatus == 0 ? "已提交发票申请" : res.receiptStatus == 1 ? "已开发票" : "-"
                    }
                }
                , { field: 'email', title: '邮箱' }
                , { field: 'receiptUserName', title: '姓名' }
                , { field: 'phone', title: '电话' }
                , { field: 'address', title: '地址' }
                , { field: 'createTime', title: '申请时间' }
                , { field: 'receiptTime', title: '开票时间' }
                , { field: 'receiptUrl', title: '电子发票地址' }
                , { title: '操作', width: 80, toolbar: '#barDemo' }
            ]],
            page: true
            , done: function () {

            }
        });

        laydate.render({
            elem: '#test1'
            , range: true
        });

        // 搜索
        form.on("submit(searchForm)", function (e) {

            for (let key in e.field) {
                if (!e.field[key]) {
                    delete e.field[key]
                }
            }

            if (e.field.createTimeStart) {
                var a = e.field.createTimeStart.split(" - ");
                e.field.createTimeStart = new Date(a[0]).getTime();
                e.field.createTimeEnd = new Date(a[1]).getTime();
            }

            table.reload('invoiceTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: e.field
            });

            return false;
        })


        // 搜索
        form.on("submit(reset)", function (e) {

            table.reload('invoiceTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: null
            });

            form.val("resets", {
                phone: "",
                receiptUserName: "",
                createTimeStart: "",
            });

            return false;
        })


        // 筛选状态
        form.on("select(select)", function (e) {
            table.reload('invoiceTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: {
                    receiptStatus: e.value
                }
            });
        })

        // 编辑和删除
        table.on("tool(invoiceTable)", function (data) {
            switch (data.event) {
                case "lookDetails":
                    layer.open({
                        type: 1,
                        title: "更新状态",
                        area: ['560px', '595px'], //宽高
                        content: `
                                <form class="layui-form" action="">
                                    <div class="layui-form-item" style="margin-top: 30px">
                                        <label class="layui-form-label">上传发票:</label>
                                        <div class="layui-upload-drag" id="uploadimg">
                                            <i class="layui-icon"></i>
                                            <p>点击上传，或将文件拖拽到此处</p>
                                            <div class="layui-hide" id="uploadDemoView">
                                                <hr>
                                                <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                            </div>
                                        </div>
                                    </div>

                                    <input type="hidden" name="id" value="${data.data.id}" />
                                    
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">发票地址</label>
                                        <div class="layui-input-block">
                                            <input style="width:400px;" type="text" value="${data.data.receiptUrl ? data.data.receiptUrl : ''}" name="receiptUrl" lay-verify="required" lay-reqtext="必填项，岂能为空？" placeholder="请输入URL地址" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">是否开发票:</label>
                                        <div class="layui-input-block">
                                            <input type="radio" name="receiptStatus" value="0" title="不开" ${!data.data.receiptStatus ? "checked" : ""} />
                                            <input type="radio" name="receiptStatus" value="1" title="开" ${data.data.receiptStatus == 1 ? "checked" : ""} />
                                        </div>
                                    </div>
                                
                                    <div class="layui-form-item" >
                                        <div class="layui-input-block">
                                            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                                        </div>
                                    </div>
                                </form>
                            `,
                        success: function () {
                            form.render();
                            uploadsFile(upload, '#uploadimg', function (res) {
                                $('input[name=receiptUrl]').val(res);
                            });

                            form.on("submit(demo1)", function (el) {
                                delete el.field.file;

                                ajax({
                                    path: "/orderReceipt/updateOrderReceipt",
                                    data: el.field,
                                    success: function (res) {
                                        layer.closeAll();
                                        layer.msg("操作成功");
                                        table.reload("invoiceTable");
                                    }
                                }, $)
                                return false;
                            })
                        }
                    });
                    break;
            }
        })

    });
</script>

</html>